//
//  RecColCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/7.
//

import UIKit

class RecColCell: UICollectionViewCell {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(4)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var disLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 9)
        label.textColor = .white
        label.backgroundColor = .init(named: "red_FE95B4")
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        label.numberOfLines = 3
        return label
    }()
    
    lazy var authorLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 11)
        label.textColor = .init(named: "gray_666666")
        return label
    }()
    
    lazy var countBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 10)
        btn.setImage(UIImage(named: "discover_hot"), for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        return btn
    }()

    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 11)
        label.textColor = .init(named: "gray_999999")
        label.numberOfLines = 0
        return label
    }()
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                authorLabel.text = m.authorname
                countBtn.setTitle("\(m.readings)".wn_wan(), for: .normal)
                desLabel.text = m.des
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bookImage)
        bookImage.addSubview(disLabel)
        addSubview(nameLabel)
        addSubview(countBtn)
        addSubview(authorLabel)
        addSubview(desLabel)
        bookImage.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
            make.height.equalTo((ScreenW-36)*0.5*1.55)            
        }
        disLabel.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.right.equalTo(-5)
            make.height.equalTo(14)
            make.width.equalTo(0)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(bookImage.snp.bottom).offset(8)
        }
        countBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(authorLabel.snp.centerY)
            make.right.equalTo(0)
            make.height.equalTo(10)
        }
        authorLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(4)
            make.left.equalTo(0)
            make.right.equalTo(-16)
        }
        desLabel.snp.makeConstraints { (make) in
            make.top.equalTo(authorLabel.snp.bottom).offset(4)
            make.left.right.bottom.equalTo(0)
        }
        countBtn.layoutButton(style: .left, imageTitleSpace: 2)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
